//
//  PhoneLoginVC.swift
//  NewXueBa
//
//  Created by Shine on 2019/6/17.
//  Copyright © 2019 jmxQAQ. All rights reserved.
//

import UIKit

class PhoneLoginVC: BCBaseVC {

    @IBOutlet weak var phoneTF: UITextField!
    @IBOutlet weak var loginBtn: BCUIButton!
    
    let dispose = DisposeBag()
    let vm = LoginVM()

    override func viewDidLoad() {
        super.viewDidLoad()
        self.fd_prefersNavigationBarHidden = true //隐藏导航栏
        phoneTF.delegate = self
        //账号的观察转换 $0.validateText(.PhoneNumber)
        let _ = phoneTF.rx.text.orEmpty
            .map{ $0.count==11 }
            .subscribe(onNext: { [weak self] (state) in
                if state {
                   self?.loginBtn.isUserInteractionEnabled = true
                   self?.loginBtn.backgroundColor = UIColor.hex(hexString: "#2E3D4B")
                } else {
                   self?.loginBtn.isUserInteractionEnabled = false
                   self?.loginBtn.backgroundColor = UIColor.hex(hexString: "#D6DCE7")
                }
            })
    }
    
    @IBAction func backClick(_ sender: UIButton) {
        self.dismiss(animated: true, completion: nil)
    }
    
    
    @IBAction func loginBtnClick(_ sender: BCUIButton) {
        if phoneTF.text!.validateText(.PhoneNumber) {
            request()
        } else {
            showTipMsg("手机号码不正确", time: 1)
        }
    }
    
    fileprivate func request() {
       
        let verifyStr = String(phoneTF.text!.prefix(4))
        let verify_code = phoneTF.text! + "#" + verifyStr + "#" + BC_Ver + "#" + BC_Ctype
        vm.getMsgMode(phoneTF.text ?? "", verify_code.md5.uppercased(), BC_Ver, BC_Ctype) { (bcCode) in
            self.manageCode(bcCode)
        }
    }
    
    fileprivate func manageCode( _ bcCode: NSInteger) {
        switch bcCode {
        case 0:
            print("成功")
            let vc = UIStoryboard.viewController(from: .LoginSB, viewControllerClass: LoginVerificationVC.self)
            vc.inputPhone = phoneTF.text!
            self.navigationController?.pushViewController(vc, animated: true)
        case 40:
            print("发送验证码失败")
            self.showTipMsg("发送验证码失败", time: 1)
        case 41:
            print("手机号码处于黑名单中")
            self.showTipMsg("手机号码处于黑名单中", time: 1)
        case 42:
            print("超过今日短信发送数目")
            self.showTipMsg("超过今日短信发送数目", time: 1)
        case 43:
            print("验证码发送频繁，请稍后再试")
            self.showTipMsg("验证码发送频繁，请稍后再试", time: 1)
        default:
            break
        }
    }
    
}

extension PhoneLoginVC: UITextFieldDelegate {
    
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
       
        if string.hasSuffix(" ") {
            return false
        }
        
        let toBeString = textField.text ?? "" + string
        if toBeString.count <= 11 {
            return true
        }else{
            return false
        }
        
    }
}
